<template>
  <div class="ValidCode disabled-select" style="width: 100%; height: 100%" @click="refreshCode">
    <span
        v-for="(item, index) in codeList"
        :key="index"
        :style="getStyle(item)"
    >
      {{ item.code }}
    </span>
  </div>
</template>

<script>
export default {
  name: "ValidCode",
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      length: 4,
      codeList: []
    };
  },
  mounted() {
    this.createdCode();
  },
  methods: {
    refreshCode() {
      this.createdCode();
    },
    createdCode() {
      const len = this.length;
      const codeList = [];
      const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789';
      const charsLen = chars.length;

      for (let i = 0; i < len; i++) {
        const rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)];
        const fontSize = `${18 + Math.floor(Math.random() * 5)}px`; // 随机字体大小

        codeList.push({
          code: chars.charAt(Math.floor(Math.random() * charsLen)),
          color: `rgb(${rgb})`,
          fontSize: fontSize,
          padding: `${Math.floor(Math.random() * 10)}px`,
          transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
        });
      }

      this.codeList = codeList;
      this.$emit('update:value', codeList.map(item => item.code).join(''));
    },
    getStyle(item) {
      return `color: ${item.color}; font-size: ${item.fontSize}; padding: ${item.padding}; transform: ${item.transform}`;
    }
  }
};
</script>

<style scoped>
.ValidCode {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.ValidCode span {
  display: inline-block;
  font-size: 18px;
}
</style>